package sanrenxing.foreMain.servlet;

import com.google.gson.Gson;
import sanrenxing.foreMain.dao.StudentSelfMsgDao;
import sanrenxing.foreMain.dao.impl.StudentSelfMsgDaoImpl;
import sanrenxing.foreMain.domain.StudentSelfMsg;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Administrator
 * @date 2020/9/29 10:53
 * 编辑者：邓志强
 * 功能：
 */
@WebServlet("/StudentSelfMsgServlet")
public class StudentSelfMsgServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求字符集
        req.setCharacterEncoding("utf-8");
        //设置响应字符集和内容类型
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        //创建输出流
        PrintWriter out=resp.getWriter();
        HttpSession session=req.getSession(true);
        //创建数据层对象，调用业务方法
        StudentSelfMsgDao ssmd=new StudentSelfMsgDaoImpl();
        StudentSelfMsg ssm=ssmd.findSelfMsg((String) session.getAttribute("stuId"));
        //判断是否有返回值
        if (ssm!=null){
            //将对象序列化成json数据
            Gson gson=new Gson();
            String json=gson.toJson(ssm);
            out.print(json);
        }else{
            out.print("no");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
